import re
import os

def replace_plt_show(file_path, filename):
    basename = filename.replace(".py", "")
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    pattern = re.compile(r'\bplt\.show\(\)')
    replacements = ['plt.savefig("{}_output{}.png")'.format(basename, i + 1) for i in range(len(re.findall(pattern, content)))]

    for replacement in replacements:
        content = re.sub(pattern, replacement, content)

    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)

def process_directory(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.py'):
                file_path = os.path.join(root, file)
                replace_plt_show(file_path,file)

if __name__ == '__main__':
    os.getcwd()
    directory = ''  # 替换为你的文件夹路径
    process_directory(os.getcwd()+'/'+directory)